package com.example.demo.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class SpringAIConfig {
    String text = """
            你是一个智能助手，你的任务是简洁回答用户的问题。
            你可以使用context里的信息来回答用户的问题,context里的内容是知识库里预先准备的：
            """;

    @Bean
    public ChatClient chatClient(ChatClient.Builder builder,
            Advisor messageChatMemoryAdvisor) {
        return builder
                .defaultSystem(
                        text)
                .defaultAdvisors(new SimpleLoggerAdvisor(), messageChatMemoryAdvisor)
                .build();
    }

    /**
     * 创建并返回一个VectorStore的Spring Bean实例。
     *
     * @param embeddingModel 向量模型
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }

    /**
     * 创建并返回一个ChatMemory的Spring Bean实例。
     *
     * @param chatMemoryRepository 聊天内存存储库
     */
    /*
     * @Bean
     * public ChatMemory chatMemory() {
     * InMemoryChatMemoryRepository inMemoryChatMemoryRepository = new
     * InMemoryChatMemoryRepository();
     * MessageWindowChatMemory messageWindowChatMemory =
     * MessageWindowChatMemory.builder()
     * .chatMemoryRepository(inMemoryChatMemoryRepository)
     * .maxMessages(10)
     * .build();
     * 
     * return messageWindowChatMemory;
     * }
     */

    /**
     * 创建并返回一个ChatMemoryRepository的Spring Bean实例。
     *
     * @return 聊天内存存储库
     */
    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory) {
        return new MessageChatMemoryAdvisor(chatMemory);
    }
}
